<?php

/**
 * View  LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    9-feb-2012
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script src="http://malsup.github.com/jquery.form.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function() {
//
        $("span.loader").attr('style', 'visibility:hidden');
        
        $("#post_image").rules("add", {
            required: <?php echo $this->data['post_image_ext'] ? 'false' : 'true' ?>,
            accept: "png|jpe?g|gif|bmp",
            messages: {
		    required: "<?php echo ERR_EMPTY ?>",
		    accept: "<?php echo ERR_EXTENSION ?>"
            }
        });
        
        $.fn.ajaxSubmit.debug = true;
        
        $("#uploaded-image-preview-box").hide();
        
        $("#post_image").change(
            function(){

            // submit the form 
            $('#postForm').ajaxSubmit({
                url:        '/admin/ajax/upload/postimage/post_id/<?php echo $this->data['post_id'] ?>', 
                beforeSubmit: function(a,f,o) {
                    o.dataType = 'json';
                    //$('#uploadOutput').html('Submitting...');
                    $(".loader").attr('style', 'visibility:visible');            
                },
                success: function(data) {
                    $(".loader").fadeOut();
                    
                    if(!data.error)
                    {
                        $("#image-upload").removeClass('error');
                        $("input#actual").attr('checked', false);
                        $("input#new").attr('checked', true);
                        $("#uploaded-image-preview-box").show();   
                        $("#uploaded-image-preview-box img").attr('src', '/uploads/temp/'+data.file_name+ "?" + (new Date()).getTime());
                        $("button.positive").removeAttr('disabled');
                    }
                    else
                    {
                        $("#image-upload").addClass('error');
                        $("#image-upload p.error").html(data.error);
                    }
                    //$out.append('<div><pre>'+ data.message +'</pre></div>');
                }
            });
            
            // return false to prevent normal browser submit and page navigation 
            return false; 
            }
        );
       
    });
    
    //]]>
</script>
<div class="info">
    <h2>Immagine Post</h2>
    <div>Per inserire un nuovo post...</div>
    <div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
</div>

    <form class="fab" name="postForm" id="postForm" action="/admin/posts/immagine/id/<?php echo $this->post_id ?>" method="post" enctype="multipart/form-data">
<div id="content">
    <ul>
        <li class="paging-context ">
            <table class="pgStyle1 circle2" cellspacing="0">
                <tr>
                    <td class="c">
                        <var>1</var>
                    </td>
                    <td class="t">
                        <b id="pagecircle1"><a href="/admin/posts/info/id/<?php echo $this->post_id ?>">Post</a></b>
                    </td>
                    <td class="c selected">
                        <var>2</var>
                    </td>
                    <td class="t selected">
                        <b id="pagecircle2">Immagine</b>
                    </td>
                    <td class="c">
                        <var>3</var>
                    </td>
                    <td class="t">
                        <b id="pagecircle2">Anteprima</b>
                    </td>                           
                </tr>
            </table>
        </li>   
        <li class="section">
            <h3><?php echo $this->data['post_title'] ?></h3>
        </li>
        <li class="complex <?php echo $this->errorMsg['post_image'] ? 'error' : '' ?>" id="image-upload">
            <label class="desc <?php echo $this->errorMsg['post_image'] ? 'error' : '' ?>">Immagine Principale</label>
            <div>
                <span class="input">
                    <input class="field file" type="file" name="post_image" id="post_image" value="" />
                </span>
            </div>
            
            <p class="error" style="<?php echo $this->errorMsg['post_image'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['post_image'] ?></p>
            <p id="instruct3" class="instruct">
                <small>I formati ammessi sono
                    <br/><strong>PNG</strong>, <strong>JPG</strong>, <strong>GIF</strong> e <strong>BMP</strong></small>
            </p>
        </li>
        <li>
                            <span class="loader">Sto caricando...</span>
        </li>
<!--        
        <li class="complex" id="image-preview">
            <div>
            <p id="instruct3" class="instruct img-preview">
                <small><strong>Immagine Attuale</strong></small><br/>
                <img src="/uploads/posts/post_<?php echo $this->data['post_id']?>.<?php echo $this->data['post_image_ext']?>" alt="Immagine Principale" title="Immagine Principale" />
            </p>
            </div>
        </li>-->
    </ul>
        <?php if( !is_null( $this->data['post_image_ext'] ) ) : ?>
        <div class="image-preview-box">
            <label for="actual">
                <small><strong>Immagine Attuale</strong></small><br/>
                <img src="/uploads/posts/post_<?php echo $this->data['post_id']?>.<?php echo $this->data['post_image_ext']?>" alt="Immagine Principale" title="Immagine Principale" />
            <br/>	
            </label>
            <input id="actual" name="image" class="field radio" type="radio" value="actual" checked="checked" />
        </div>        
        <?php endif; ?>
        
        <!--  Questo Box viene nascosto al caricamento della pagina       -->
        <div id="uploaded-image-preview-box" class="image-preview-box">
            <label for="new">
                <small><strong>Nuova Immagine</strong></small><br/>
                <img src="/uploads/posts/post_<?php echo $this->data['post_id']?>.<?php echo $this->data['post_image_ext']?>" alt="Immagine Principale" title="Immagine Principale" />
            <br/>	
            </label>
            <input id="new" name="image" class="field radio" type="radio" value="new" />
        </div>
        <br class="clear"/>

</div>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <div id="formButtons">
        <button id="salva" type="submit" name="salva" value="salva" class="button positive">
            <img src="/images/admin/tick.png" alt="" />Continua
        </button>
        <button id="annulla" name="annulla" value="annulla" class="button negative">
            <img src="/images/admin/cross.png" alt="" />Annulla
        </button>
    </div>
</form>
